package vn.hanelsoft.openwifi.model.entity;

import java.util.ArrayList;

import vn.hanelsoft.openwifi.model.handler.DBDefinition;
import vn.hanelsoft.openwifi.model.object.BaseObject;
import vn.hanelsoft.openwifi.model.object.ObjectWifi;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class EntityWifi extends EntityBase {

	public EntityWifi(Context mContext) {
		super(DBDefinition.TABLE_WIFIS, mContext);
	}

	@Override
	public long add(BaseObject _obj) {
		// TODO Auto-generated method stub
		int count = getCount();
		if(count>=10)
			return 0;
		ObjectWifi obj = (ObjectWifi) _obj;
		if (obj != null) {
			ContentValues values = new ContentValues();
			values.put(DBDefinition.COLUMN_WIFI_SSID, obj.getWifiSSID());
			values.put(DBDefinition.COLUMN_WIFI_PASSWORD, obj.getWifiPassword());
			values.put(DBDefinition.COLUMN_WIFI_BSSID, obj.getWifiBSSID());
			values.put(DBDefinition.COLUMN_WIFI_EMAIL, obj.getEmail());
			values.put(DBDefinition.COLUMN_WIFI_LAT, obj.getLat());
			values.put(DBDefinition.COLUMN_WIFI_LNG, obj.getLng());
			values.put(DBDefinition.COLUMN_WIFI_STATUSCONNECT,
					obj.getStatusConnect());
			values.put(DBDefinition.COLUMN_WIFI_TYPEWIFI, obj.getTypeWifi());
			values.put(DBDefinition.COLUMN_WIFI_FACEBOOKID, obj.getFacebookId());
			values.put(DBDefinition.COLUMN_WIFI_FACEBOOKNAME,
					obj.getFacebookName());
			values.put(DBDefinition.COLUMN_WIFI_FACEBOOKIMG,
					obj.getFacebookImg());

			return super.add(values);
		} else {
			return 0;
		}
	}

	@Override
	public boolean update(BaseObject _obj) {
		// TODO Auto-generated method stub
		ObjectWifi obj = (ObjectWifi) _obj;
		if (obj != null) {
			ContentValues values = new ContentValues();
			values.put(DBDefinition.COLUMN_WIFI_SSID, obj.getWifiSSID());
			values.put(DBDefinition.COLUMN_WIFI_PASSWORD, obj.getWifiPassword());
//			values.put(DBDefinition.COLUMN_WIFI_BSSID, obj.getWifiBSSID());
			values.put(DBDefinition.COLUMN_WIFI_EMAIL, obj.getEmail());
			values.put(DBDefinition.COLUMN_WIFI_LAT, obj.getLat());
			values.put(DBDefinition.COLUMN_WIFI_LNG, obj.getLng());
			values.put(DBDefinition.COLUMN_WIFI_STATUSCONNECT,
					obj.getStatusConnect());
			values.put(DBDefinition.COLUMN_WIFI_TYPEWIFI, obj.getTypeWifi());
			values.put(DBDefinition.COLUMN_WIFI_FACEBOOKID, obj.getFacebookId());
			values.put(DBDefinition.COLUMN_WIFI_FACEBOOKNAME,
					obj.getFacebookName());
			values.put(DBDefinition.COLUMN_WIFI_FACEBOOKIMG,
					obj.getFacebookImg());

			String strWhere = DBDefinition.COLUMN_WIFI_BSSID + " = '"
					+ obj.getWifiBSSID()+"'";

			return super.update(values, strWhere, null);
		} else {
			return false;
		}
	}

	@Override
	public int delete(BaseObject _obj) {
		// TODO Auto-generated method stub
		ObjectWifi obj = (ObjectWifi) _obj;
		if (obj != null) {
			String where = DBDefinition.COLUMN_WIFI_BSSID + " = "
					+ obj.getWifiBSSID();
			return super.delete(where, null);
		} else
			return 0;
	}
	
	public int deleteWifiSave(ObjectWifi _obj) {
		if (_obj != null) {
			String where = DBDefinition.COLUMN_WIFI_BSSID + " = '"
					+ _obj.getWifiBSSID() +"'";
			return super.delete(where, null);
		} else
			return 0;
	}

	@Override
	public BaseObject getById(String id) {
		// TODO Auto-generated method stub
		ObjectWifi obj = new ObjectWifi();
		try {
			if (null != _db)
				_db = null;
			_db = _dbhandler.getWritableDatabase();
			String strWhere = DBDefinition.COLUMN_WIFI_SSID + " = " + id;
			Cursor mCursor = _db.query(_tblname, null, strWhere, null, null,
					null, null);
			if (mCursor != null && mCursor.moveToFirst()) {
				obj.setWifiSSID(mCursor.getString(1));
				obj.setWifiPassword(mCursor.getString(2));
				obj.setWifiBSSID(mCursor.getString(3));
				obj.setEmail(mCursor.getString(4));
				obj.setLat(mCursor.getString(5));
				obj.setLng(mCursor.getString(6));
				obj.setStatusConnect(mCursor.getString(7));
				obj.setTypeWifi(mCursor.getString(8));
				obj.setFacebookId(mCursor.getString(9));
				obj.setFacebookName(mCursor.getString(10));
				obj.setFacebookImg(mCursor.getString(11));
			}
			mCursor.close();
			_db.close();
		} catch (Exception e) {
		}
		return obj;
	}
	
	public boolean checkWifi(String strBSSID) {
		try {
			if (null != _db)
				_db = null;
			_db = _dbhandler.getWritableDatabase();
			Cursor mCursor = _db.query(_tblname, null, null, null, null, null,
					null);
			if (mCursor != null) {
				mCursor.moveToFirst();
				do {
					String oldBSSID = mCursor.getString(3);
					if(oldBSSID.equals(strBSSID))
					{
						return true;
					}
				} while (mCursor.moveToNext());
			}
			mCursor.close();
			_db.close();
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public ArrayList<ObjectWifi> getListWifiSaveAll() {
		ArrayList<ObjectWifi> listObject = new ArrayList<ObjectWifi>();
		try {
			if (null != _db)
				_db = null;
			_db = _dbhandler.getWritableDatabase();
			Cursor mCursor = _db.query(_tblname, null, null, null, null, null,
					null);
			if (mCursor != null) {
				mCursor.moveToFirst();
				do {
					ObjectWifi obj = new ObjectWifi();
					obj.setWifiSSID(mCursor.getString(1));
					obj.setWifiPassword(mCursor.getString(2));
					obj.setWifiBSSID(mCursor.getString(3));
					obj.setEmail(mCursor.getString(4));
					obj.setLat(mCursor.getString(5));
					obj.setLng(mCursor.getString(6));
					obj.setStatusConnect(mCursor.getString(7));
					obj.setTypeWifi(mCursor.getString(8));
					obj.setFacebookId(mCursor.getString(9));
					obj.setFacebookName(mCursor.getString(10));
					obj.setFacebookImg(mCursor.getString(11));
					listObject.add(obj);
				} while (mCursor.moveToNext());
			}
			mCursor.close();
			_db.close();
		} catch (Exception e) {
			listObject.clear();
		}
		return listObject;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		int count = 0;
		try {
			if (null != _db)
				_db = null;
			_db = _dbhandler.getWritableDatabase();
			Cursor mCursor = _db.query(_tblname, null, null, null, null, null,
					null);
			if (mCursor != null) 
				count = mCursor.getCount();
			mCursor.close();
			_db.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public ArrayList<BaseObject> getAll() {
		// TODO Auto-generated method stub
		return null;
	}
}
